package com.traffic.managent.plugin;

import java.util.List;

import org.apache.log4j.Logger;

import com.traffic.managent.jedis.RedisKit;
import com.traffic.managent.model.customer.Customer;
import com.traffic.managent.model.phone.PhoneHead;
import com.traffic.managent.model.product.Provider;
import com.traffic.managent.model.system.Operator;
import com.traffic.managent.model.system.Role;
import com.traffic.managent.model.system.User;
import com.traffic.managent.util.GetLocalIPUtil;

/**
 * 系统初始化缓存操作类
 * 
 */
public class ParamInitThread extends Thread {

	private static Logger log = Logger.getLogger(ParamInitThread.class);

	public static String cacheStart_user = "user_";
	public static String cacheStart_group = "group_";
	public static String cacheStart_role = "role_";
	public static String cacheStart_station = "station_";
	public static String cacheStart_operator = "operator_";
	public static String cacheStart_dict = "dict_";
	public static String cacheStart_dict_child = "dict_child_";
	public static String cacheStart_param = "param_";
	public static String cacheStart_param_child = "param_child_";
	public static String cacheStart_interfaces = "interfaces_";
	public static String cacheStart_cust_interfaces = "cust_interfaces_";

	public static String cacheStart_customer = "customer";
	public static String cacheStart_appkey = "appkey_"; // appkey与privateKey关联
	public static String cacheStart_bindips = "bindips_"; // 将appkey与客户绑定的bindIps关联

	public static String cacheStart_provider = "provider";
	public static String cacheStart_supplier = "supplier";

	// public static String cacheStart_phone_head = "phone_head_";
	// 包含省市区
	public static String cacheStart_phone_heads = "phone_heads_";

	public static String cacheStart_order_status_customerNo = "orderno_";

	public static String cacheStart_order_status_orderNo = "orderid_";

	public static String cacheStart_order_delay = "delay_";

	public static String cacheStart_front_order_delay = "front_delay_";

	public static String cacheStart_present_order_delay = "present_delay_";

	public static String cacheStart_register_sms = "reg_sms_";
	public static String cacheStart_modify_password_sms = "modify_sms_";
	public static String cacheStart_find_password_sms = "find_sms_";

	@Override
	public void run() {
		cacheAll();
	}

	/**
	 * 缓存所有参数
	 */
	public static synchronized void cacheAll() {
		log.info("缓存参数初始化 start ...");
		// 7.缓存客户
				platform_cacheCustomer();
		// 1.缓存用户
		platform_cacheUser();

		// 2.缓存角色
		platform_cacheRole();

		// 4.缓存功能
		platform_cacheOperator();

		

		// 8缓存APPKEY 需要优化地方：APPKEY和securityKey如果有变更 需要重启 刷新内存
		platform_cacheAppkey();
		// 9缓存供应商信息
		platform_cachProvider();
		// 10，缓存号段
		platform_cachPhonesNo();
		// 11，缓存服务ip
		platform_cachserverIP();

		// platform_cachToDelay();

		log.info("缓存参数初始化 end ...");
	}

	/**
	 * 1,缓存所有用户
	 * 
	 */
	public static void platform_cacheUser() {
		log.info("缓存加载：CxUser start");
		String sql = "select ids from cx_user where status = '1' ";
		List<User> userList = User.dao.find(sql);
		for (User user : userList) {
			User.dao.cacheAdd(user.getStr("ids"));
			user = null;
		}
		log.info("缓存加载：CxUser end, size = " + userList.size());
		userList = null;
	}

	/**
	 * 2,缓存所有角色
	 * 
	 */
	public static void platform_cacheRole() {
		log.info("缓存加载：CxRole start");
		String sql = "select ids from cx_role ";
		List<Role> roleList = Role.dao.find(sql);
		for (Role role : roleList) {
			Role.dao.cacheAdd(role.getStr("ids"));
		}
		log.info("缓存加载：CxRole end, size = " + roleList.size());
		roleList = null;
	}

	/**
	 * 4，缓存功能操作
	 * 
	 */
	public static void platform_cacheOperator() {
		log.info("缓存加载：CxOperator start");
		String sql = "select ids from cx_operator ";
		List<Operator> operatorList = Operator.dao.find(sql);
		for (Operator operator : operatorList) {
			Operator.dao.cacheAdd(operator.getStr("ids"));
			operator = null;
		}
		log.info("缓存加载：CxOperator end, size = " + operatorList.size());
		operatorList = null;
	}

	/**
	 * 7,缓存所有客户信息
	 * 
	 */

	public static void platform_cacheCustomer() {
		log.info("缓存加载：Customerinfo start");
		String sql = "select * from cx_customer_info where  delFlag = 0 and status in ('1','3','4') ";
		List<Customer> customerList = Customer.dao.find(sql);
		for (Customer customer : customerList) {
			Customer.dao.cacheAdd(customer.getStr("customerId"));
			customer = null;
		}
		log.info("缓存加载：Customerinfo end, size = " + customerList.size());
		customerList = null;
	}

	/**
	 * 8，缓存所有用户的appkey
	 * 
	 */
	public static void platform_cacheAppkey() {
		log.info("缓存加载：CxUser start");
		String sql = "select ids from cx_user where status = '1' ";
		List<User> userList = User.dao.find(sql);

		for (User user : userList) {
			String uid = user.getStr("ids");
			User u = RedisKit.get(cacheStart_user + uid);
			if (u != null) {
				String cid = u.getStr("customerId");
				Customer c = RedisKit.get(cacheStart_customer + cid);
				if (c != null) {
					// 将私钥跟appkey映射放入缓存中
					RedisKit.put(cacheStart_appkey + u.getStr("username"), c.getStr("privateKey"));
					// 将bindIps跟appkey映射放入缓存中
					RedisKit.put(cacheStart_bindips + user.getStr("username"), c.getStr("bindIps"));
				}

			}
		}
		log.info("缓存加载：CxUser end, size = " + userList.size());
		userList = null;
	}

	/**
	 * 9,缓存供應商信息
	 */
	public static void platform_cachProvider() {
		log.info("缓存加载：Providerinfo start");
		List<Provider> providers = Provider.dao.findAll();
		for (Provider provider : providers) {
			Provider.dao.cacheAdd(provider);
			provider = null;
		}
		log.info("缓存加载：Providerinfo end, size = " + providers.size());
		providers = null;
	}

	/**
	 * 10 缓存号段信息，包含省市区
	 */
	public static void platform_cachPhonesNo() {
		Long now = System.currentTimeMillis();
		log.info("缓存加载省市区：phone head start");
		List<PhoneHead> heads = PhoneHead.dao.findProvinceArea();
		for (PhoneHead pHead : heads) {
			PhoneHead.dao.cacheAdd(pHead.getStr("phoneSeg"), pHead.getStr("cvalues"));
		}
		log.info("缓存加载省市区：phone head end, size = " + heads.size());
		log.info("缓存加载省市区：phone head end, time = " + (System.currentTimeMillis() - now) / 1000 + " s.");
		heads = null;
	}

	/**
	 * 11缓存服务器ip
	 */
	public static void platform_cachserverIP() {
		log.info("缓存服务器ip：phone head start");
		RedisKit.put("serverlocalhostip", GetLocalIPUtil.getLocalIP());
		log.info("缓存服务器ip：phone head end" + GetLocalIPUtil.getLocalIP());
	}

	/**
	 * 将redis里面的数据加载到本地缓存中
	 */
	// public static void platform_cachToDelay() {
	// GoodsCacheDelay.service.addCacheFromRedis();
	// // ForwardGoodsCacheDelay.service.addCacheFromRedis();
	// }

}
